#include <iostream>
#include <map> //注意：STL头文件没有拓展名.h
#include "A.h"

using namespace std;

void A::Say()
{
    std::cout << "Hello world" << std::endl;
}

//<l1>two sum
void A::L1TwoSum()
{
    int nums[] = {2, 7, 11, 15};
    int l = sizeof(nums) / sizeof(int);
    int target = 0;

    cout << "原数组为:";
    for (int i = 0; i < l; i++)
    {
        cout << nums[i] << ",";
    }

    cout << endl;
    cout << "输入：" << endl;
    cin >> target;

    for (int i = 0; i < l; i++)
    {
        // if(nums[i]>target)//注释原因：不能针对负数
        // {
        //     continue;
        // }
        // else
        {
            for (int j = i + 1; j < l; j++)
            {
                if (nums[i] + nums[j] == target) //这里越界，当i取最后一位，此时j越界
                {
                    cout << "i,j:" << i << "," << j << endl;
                    continue;
                }
            }
        }
    }
}

//L1
vector<int> A::TwoSum(vector<int> &nums, int target)
{
    map<int, int> a;
    vector<int> b(2, -1); //初始化一个大小为2，值为-1的容器
    for (int i = 0; i < nums.size(); i++)
    {
        if (a.count(target - nums[i]) > 0)
        {
            b[0] = a[target - nums[i]];
            b[1] = i;
            break;
        }
        a[nums[i]] = i;
    }

    for (const int &k : b)
    {
        cout << k << " , ";
    }
    cout << endl;
    return b;
}

//L2
void A::L2LinkSum()
{
    struct node *l1;
    cout << "输入";
    int cinData = 0;

    for (int i = 0; i < 3; i++)
    {
        cin >> cinData;
        node p;
        p.data = cinData;
        l1->next = &p;
    }

    cout<<"显示："<<endl;

    while(l1!=NULL)
    {
        printf("%d\n",l1->data);
        l1=l1->next;
    }
}